﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace FileDuplicateUtility
{
    public class Progress : DependencyObject
    {
        public bool IsIndeterminate
        {
            get { return (bool)GetValue(IsIndeterminateProperty); }
            set { SetValue(IsIndeterminateProperty, value); }
        }

        public static readonly DependencyProperty IsIndeterminateProperty =
            DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(Progress), new UIPropertyMetadata(false));

        public int Total
        {
            get { return (int)GetValue(TotalProperty); }
            set { SetValue(TotalProperty, value); }
        }

        public static readonly DependencyProperty TotalProperty =
            DependencyProperty.Register("Total", typeof(int), typeof(Progress), new UIPropertyMetadata(100));

        public int Current
        {
            get { return (int)GetValue(CurrentProperty); }
            set { SetValue(CurrentProperty, value); }
        }

        public static readonly DependencyProperty CurrentProperty =
            DependencyProperty.Register("Current", typeof(int), typeof(Progress), new UIPropertyMetadata(0, Current_PropertyChanged));

        private static void Current_PropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var obj = (sender as Progress);
            if (obj.Total > 0)
            {
                obj.Percentage = obj.Current * 100.0 / obj.Total;
            }
        }

        public double Percentage
        {
            get { return (double)GetValue(PercentageProperty); }
            private set { SetValue(PercentageProperty, value); }
        }

        public static readonly DependencyProperty PercentageProperty =
            DependencyProperty.Register("Percentage", typeof(double), typeof(Progress), new UIPropertyMetadata(0d));

        public void Reset()
        {
            Total = 100;
            Current = 0;
            IsIndeterminate = false;
        }

        public override string ToString()
        {
            var sb = new StringBuilder();

            if (IsIndeterminate)
            {
                sb.Append("Unknown");
            }
            else
            {
                sb.AppendFormat("{0} %", Percentage);

                if (Total > 0)
                {
                    sb.AppendFormat(" ({0} / {1})", Current, Total);
                }
            }

            return sb.ToString();
        }
    }
}
